package com.xqboss.apps.service.nft;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.nft.NftBeforeRule;
import com.xqboss.apps.dto.nft.HoldPictureDTO;
import com.xqboss.apps.mapper.nft.NftBeforeRuleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HaoLun
 * @since 2022-09-07
 */
@Service
public class NftBeforeRuleService extends ServiceImpl<NftBeforeRuleMapper, NftBeforeRule> {

    @Autowired
    private NftBeforeRuleService nftBeforeRuleService;

    /**
     * @param id              主键ID
     * @param holdPictureDTOS 持图购数据
     * @param ruleType        1藏品 2箱子 3活动 4转盘
     */
    public void addHoldPicture(Long id, List<HoldPictureDTO> holdPictureDTOS, Integer ruleType) {
        NftBeforeRule nftBeforeRule = new NftBeforeRule();
        nftBeforeRule.setNftId(id);
        nftBeforeRuleService.remove(new LambdaQueryWrapper<NftBeforeRule>().eq(NftBeforeRule::getNftId, id).eq(NftBeforeRule::getRuleType, ruleType));
        for (HoldPictureDTO holdPictureDTO : holdPictureDTOS) {
            BeanUtils.copyProperties(holdPictureDTO, nftBeforeRule);
            nftBeforeRule.setBeforeNfts(JSONObject.toJSONString(holdPictureDTO.getSchemes()));
            nftBeforeRule.setExtraRules(JSONObject.toJSONString(holdPictureDTO.getExtraRules()));
            nftBeforeRule.setRuleType(ruleType);
            nftBeforeRuleService.save(nftBeforeRule);
        }
    }

}
